/*
 *  CTrackStereo.h
 *  Gangstar2
 *
 *  Created by covey on 27/01/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#ifndef __CTrackStereo_H__
#define __CTrackStereo_H__

//#include "../Define/customHead.h"//by mliao
//#include "../Defs.h"//by mliao

#if defined(TARGET_IPHONE_SIMULATOR) || defined(TARGET_OS_IPHONE)
#include <OpenAL/al.h>
#include <OpenAL/oalStaticBufferExtension.h>
#include <OpenAL/alc.h>
extern ALvoid alBufferDataStaticProc(const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq);
#else
#include <al.h>
#endif

#include "AudioDefs.h"

//class AdpcmState;
struct SNDWaveParam;

using namespace gameloft;

// Struct that contains the pointer to our file in memory

////////////////////////////////////////////////////////////////////////
//*******************************************************************
//	class CTrackStereo:
//*******************************************************************
////////////////////////////////////////////////////////////////////////
class CTrackStereo
	{
	public:
		
		enum
		{
			NUM_BUFFER = 4,
			NUM_CHUNK_BY_BUFFER = 4,
			BUFFER_TIME_SIZE = 250,
		};
		
		enum EWaveStatus
		{
			k_waveStatus_unknown,
			k_waveStatus_initial,
			k_waveStatus_playing,
			k_waveStatus_pause,
			k_waveStatus_stop,
			k_waveStatus_count,
		};
		
		ALint iTotalBuffersProcessed,iBuffersProcessed,iQueuedBuffers;
	private:
		audio::u8		m_nChannel;	
		int				m_pan;
		int				m_rate;
		int				m_bufferSize;
		audio::OSTick	m_timeLen;
		audio::OSTick	m_totalTick;
		
		
		unsigned long   ulChannels;
		unsigned long   ulFormat;
		unsigned long   ulBytesWritten;
		ALuint uiBuffers[NUM_BUFFER],uiBuffer;
		long ulDecodeSize;
		unsigned long ulBytesDone;
		ALuint uiSource;
				
		float			savedGain;
		bool			m_isBufferInited;
		bool			m_needResync;

	private:
		EWaveStatus	TranslateWaveState(int alState)const;
	public:
		CTrackStereo();
		~CTrackStereo();
		
		bool ReInitSoundInstance(void);
		
		int Init(int channel, int rate, int bufferSize, bool looping=false);
		
		void setAlError(int error);
		int GetAlState();
		int GetQueuedBuffer();
		
		void Cleanup();
		void Play();
		void Stop(void);
		void ClearSoundFile(void);
		void Reset();
		
		void Update(audio::OSTick totalTick, audio::s8* buffer, int bufferNo = -1, int bufferSize = -1);
		bool IsFinished();
		
		void SetVolume( int vol );
		void SetPan( int pan );
		void Pause();
		void SetVolumeGain(float in_gain);
		
		bool NeedResync(){return m_needResync;}
		void SetResynced();
		
		int GetSoundPos();
		int GetSoundByteOffset();
		void SetSoundByteOffset(int byteOffset);
		int GetSoundSampleOffset();
		void SetSoundSampleOffset(int sampleOffset);
		
		int GetProcessedBuffer(void);
		
		int GetSourceState(void)const;

		void setLoop(bool loop);
		void setPitch(float pitchmod);
		void resetPitch();

		void setSourcePosition(const float x, const float y, const float z) const;
		
		inline void SetChannel(int channel);
		inline int GetChannel() const;
		
		inline int GetTimeLength() const;
	};

////////////////////////////////////////////////////////////////////////
//*******************************************************************
//INLINE FUNCTIONS
//*******************************************************************
////////////////////////////////////////////////////////////////////////
void CTrackStereo::SetChannel(int channel)
{
	m_nChannel = (audio::u8)channel;
}

////////////////////////////////////////////////////////////////////////
int CTrackStereo::GetChannel() const
{
	return m_nChannel;
}

//////////////////////////////////////////////////////////////////////////
//int CTrackStereo::GetDataPosition() const
//{
//	return m_nDataPos;
//}
//
//////////////////////////////////////////////////////////////////////////
//int CTrackStereo::GetDataLength() const
//{
//	return m_nDataLen;
//}

int CTrackStereo::GetTimeLength() const
{
	return m_timeLen;
}

//int CTrackStereo::GetTotalLength() const
//{
//	return m_filesize;
//}

//int CTrackStereo::GetChunkSize() const
//{
//	return m_nChunkSize;
//}

#endif //__CTrackStereo_H__
